package pres.wchen.oa.cor.shiro.web;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import pres.wchen.oa.cor.shiro.model.BaseModel;
import pres.wchen.oa.cor.shiro.model.SysPermission;
import pres.wchen.oa.cor.shiro.service.SysPermissionService;

/**
 * 源文件名：SysPermissionController.java
 * 作者：wchen
 * 创建时间：2018/1/14
 * 文件描述：权限资源表
 * 版权所有：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息： 
 */
@RestController
@RequestMapping("/permission")
public class SysPermissionController extends BaseController {
	@Resource
	private SysPermissionService sysPermissionService;



	/**
	 * @方法描述 新增
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@PostMapping(value = "/")
	public ModelAndView insert(SysPermission sysPermission, RedirectAttributes attributes) {
		Long id = sysPermissionService.insert(sysPermission);
		attributes.addAttribute("pageNo", 1);
		attributes.addAttribute("pageSize", 3);
		return super.dealInfoResult("redirect:/permission/pageList", null);
	}


	/**
	 * @方法描述 删除
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@DeleteMapping(value = "/{id}")
	public Map delete(@PathVariable("id") Long id) {
		Integer result = sysPermissionService.delete(id);
		return super.dealDeleteResult(result);
	}


	/**
	 * @方法描述 更新
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@PutMapping(value = "/")
	public ModelAndView update(SysPermission sysPermission, RedirectAttributes attributes) {
		Integer result = sysPermissionService.update(sysPermission);
		attributes.addAttribute("pageNo", 1);
		attributes.addAttribute("pageSize", 3);
		return super.dealInfoResult("redirect:/permission/pageList", null);
	}


	/**
	 * @方法描述 详情
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/{id}")
	public Map info(@PathVariable("id") Long id) {
		SysPermission sysPermission = sysPermissionService.info(id);
		return super.dealInfoResult(sysPermission);
	}


	/**
	 * @方法描述 列表
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@PostMapping(value = "/list")
	public Map list(Map map) {
		BaseModel baseModel = sysPermissionService.list(map);
		return super.dealListResult(baseModel);
	}


	/**
	 * @方法描述 主页
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/index")
	public ModelAndView index() {
		return super.dealInfoResult("/role/permission", new HashMap<>());
	}



	/**
	 * @方法描述 权限资源角色分页列表
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/pageList")
	public ModelAndView pageList(HttpServletRequest request) {
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", request.getParameter("pageSize"));
		conditionMap.put("pageNo", request.getParameter("pageNo"));
		conditionMap.put("permissionName", request.getParameter("permissionName"));
		conditionMap.put("pageFlag", request.getParameter("pageFlag"));
		BaseModel baseModel = sysPermissionService.list(conditionMap);
		return super.dealListResult("/role/permission_table", baseModel);
	}


	/**
	 * @方法描述 查询角色资源列表(区分是否勾选)
	 * @创建作者 wchen
	 * @方法入参 map
	 * @返回值
	 * @创建日期 2018/1/13
	 */
	@GetMapping(value = "/allotPermissions/list")
	public Map chooseUserRolesList(HttpServletRequest request) {
		Object roleId = request.getParameter("roleId");
		Object pageFlag = request.getParameter("pageFlag");
		Map conditionMap = new HashMap();
		conditionMap.put("roleId", roleId);
		conditionMap.put("pageFlag", pageFlag);
		BaseModel baseModel = sysPermissionService.chooseRolePermissionList(conditionMap);
//		return super.dealListResult("/role/allot_permission_table", baseModel);
		return super.dealListResult(baseModel);
	}
}